package com.tensquare.sms.listener;

import com.aliyuncs.exceptions.ClientException;
import com.tensquare.sms.utils.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @BelongsProject: tensquare_parent
 * @BelongsPackage: com.tensquare.sms.listener
 * @Author: yang
 * @CreateTime: 2019-03-11 16:00
 * @Description: ${Description}
 */

@Component
@RabbitListener(queues = "sms")
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil; //调用阿里短信服务工具类

    @Value("${aliyun.sms.template_code}")  //从application.yml 获取对应值
    private String template_code;
    @Value("${aliyun.sms.sign_name}")
    private String sign_name;

    @RabbitHandler
    public void executeSms(Map<String,String> map){

       String mobile=map.get("mobile");
       String checkcode=map.get("checkcode");
       System.out.println("手机号:"+map.get("mobile"));
       System.out.println("验证码:"+map.get("checkcode"));
        try {
            smsUtil.sendSms(mobile,template_code,sign_name,"{\"checkcode\":\""+checkcode+"\"}");  //阿里云 短信模板上的 自己定义的占位符 ${checkcode}
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}
